//package no.group.gowther.service;
//
//import no.group.gowther.exception.BusinessException;
//import no.group.gowther.exception.ErrorMessage;
//import no.group.gowther.service.dto.RealFilePath;
//import no.group.gowther.service.dto.VirtualFilePath;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.boot.context.properties.ConfigurationProperties;
//import org.springframework.stereotype.Service;
//
//import java.nio.file.Path;
//import java.util.List;
//import java.util.Map;
//import java.util.stream.Collectors;
//
///**
// * @author ayatsuji
// * @date 2022-04-11 22:06
// * @since
// */
//@Service
//@ConfigurationProperties("dir-config")
//public class FilePathMappingService {
//    private static final Logger LOGGER = LoggerFactory.getLogger(FilePathMappingService.class);
//    private Map<VirtualFilePath, RealFilePath> virtualToRealPathMap;
//    private Map<RealFilePath, VirtualFilePath> realToVirtualPathMap;
//    private List<VirtualFilePath> rootPaths;
//
//    public RealFilePath toRealPath(VirtualFilePath virtualFilePath) {
//        final VirtualFilePath virtualRootDir = virtualFilePath.getRootDir();
//        final RealFilePath realRootPath = virtualToRealPathMap.get(virtualRootDir);
//        if (realRootPath == null) {
//            throw BusinessException.createException(ErrorMessage.PARAM_ERROR.getErrorCode(), String.format("路径[%s]不存在", virtualFilePath.filePath()));
//        }
//        return virtualFilePath.toReal(realRootPath);
//    }
//
//    public VirtualFilePath toVirtualPath(RealFilePath realFilePath) {
//        for (RealFilePath rootRealPath: realToVirtualPathMap.keySet()) {
//            if (realFilePath.startsWith(rootRealPath)) {
//                final VirtualFilePath virtualRootPath = realToVirtualPathMap.get(rootRealPath);
//                return realFilePath.toVirtual(rootRealPath, virtualRootPath);
//            }
//        }
//        LOGGER.error("无法映射真实路径[{}]为虚拟路径", realFilePath.getFilePath());
//        throw BusinessException.createException(ErrorMessage.SYSTEM_ERROR.getErrorCode(), ErrorMessage.SYSTEM_ERROR.getErrorMsg());
//    }
//
//    public VirtualFilePath toVirtualPath(Path realFilePath) {
//        return toVirtualPath(new RealFilePath(realFilePath.toString()));
//    }
//
//    public List<VirtualFilePath> getRootDir() {
//        return rootPaths;
//    }
//
//    public void setMapping(Map<String, String> mapping) {
//        this.virtualToRealPathMap = mapping.entrySet().stream()
//                .collect(Collectors.toMap(
//                        entry -> new VirtualFilePath(entry.getKey()),
//                        entry -> new RealFilePath(entry.getValue())
//                        )
//                );
//        realToVirtualPathMap = mapping.entrySet().stream()
//                .collect(Collectors.toMap(
//                        entry -> new RealFilePath(entry.getValue()),
//                        entry -> new VirtualFilePath(entry.getKey())
//                        )
//                );
//        rootPaths = mapping.keySet().stream().map(VirtualFilePath::new).collect(Collectors.toList());
//    }
//}
